package com.cs.sso.controller;

import com.cs.common.vo.ReturnVo;
import com.cs.sso.dbo.Account;
import com.cs.sso.exception.LoginException;
import com.cs.sso.service.AccountService;
import com.cs.sso.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("account")
public class AccountController {
	@Autowired
	private AccountService accountService;

	@RequestMapping("create")
	public ReturnVo create(@RequestBody Account account) {
		ReturnVo returnVo = new ReturnVo();
		try {
			accountService.create(account);
		} catch (LoginException le) {
			return returnVo.failure(le.getMessage());
		}
		return returnVo.success();
	}
	
	@RequestMapping("remove")
	public ReturnVo remove(@RequestParam("username") String username) {
		ReturnVo returnVo = new ReturnVo();
		try {
			accountService.remove(username);
		} catch (LoginException le) {
			return returnVo.failure(le.getMessage());
		}
		return returnVo.success();
	}
	
	@RequestMapping("resetPassword")
	public ReturnVo resetPassword(@RequestParam("username") String username) {
		ReturnVo returnVo = new ReturnVo();
		String password = "";
		try {
			password = accountService.resetPassword(username);
		} catch (LoginException le) {
			return returnVo.failure(le.getMessage());
		}
		return returnVo.success(password);
	}

}
